<?php
require_once("db.php");

//---------------------------
// MakeHistoryTable
//---------------------------
function MakeHistoryTable($userid, $max_entries=0)
{
	global $total_correct, $total_problems, $total_percent;
	
	$total_correct = 0;
	$total_problems = 0;
	$total_percent = 0;

	$all = GetUserTestHistory($userid);

	$ret_str = "<table border='1'>\n";

	$ret_str .= "  <tr class='color13'>\n";
	$ret_str .= "    <th>Date</th>\n"; 
	$ret_str .= "    <th>%</th>\n"; 
	$ret_str .= "    <th>Num. Correct</th>\n"; 
	$ret_str .= "    <th>Num. Problems</th>\n"; 
	$ret_str .= "    <th>Time to complete</th>\n"; 
	$ret_str .= "    <th>Seconds per correct answer</th>\n"; 
	$ret_str .= "  </tr>\n";

	$ncolors = 3;

	$i = 0;
	foreach($all AS $row){
		
		$time_diff = $row["time_seconds"];
		$minutes = floor($time_diff/60.0);
		$seconds = $time_diff - $minutes*60.0;
		$percent = sprintf("%3.1f%%", 100.0*$row["Ncorrect"]/$row["Nprob"]);
		$seconds_per_correct_answer = "N/A";
		if($row["Ncorrect"]>0)$seconds_per_correct_answer = sprintf("%3.1f", $time_diff/$row["Ncorrect"]);
		$color = $colors[$i%$ncolors];
		$i++;
	
		$ret_str .= "  <tr class='color${i}'>\n";
		$ret_str .= "    <td align='center'>".$row["created"]."</td>\n"; 
		$ret_str .= "    <td align='center'>$percent</td>\n"; 
		$ret_str .= "    <td align='center'>".$row["Ncorrect"]."</td>\n"; 
		$ret_str .= "    <td align='center'>".$row["Nprob"]."</td>\n"; 
		$ret_str .= "    <td align='center'>${minutes}m ${seconds}s</td>\n"; 
		$ret_str .= "    <td align='center'>$seconds_per_correct_answer</td>\n"; 
		$ret_str .= "  </tr>\n";
		
		$total_correct += $row["Ncorrect"];
		$total_problems += $row["Nprob"];
		$total_percent = sprintf("%3.1f%%", 100.0*$total_correct/$total_problems);
		
		if($max_entries>0 && $i>=$max_entries)break;
	}
	
	$ret_str .= "</table>\n";
	
	return $ret_str;
}

?>